<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>工作中常用的lambda总结 | 牛牛&amp;blog</title><meta name="author" content="牛牛"><meta name="copyright" content="牛牛"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="工作中常用的lambda总结1. list转map工作中，我们经常遇到list转map的案例。Collectors.toMap就可以把一个list数组转成一个Map。代码如下： public class TestLambda &amp;#123;    public static void main(String[] args) &amp;#123;        List&lt;UserInfo&gt; use">
<meta property="og:type" content="article">
<meta property="og:title" content="工作中常用的lambda总结">
<meta property="og:url" content="https://code-ran.gitee.io/2022/11/23/%E5%B7%A5%E4%BD%9C%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%9A%84lambda%E6%80%BB%E7%BB%93/index.html">
<meta property="og:site_name" content="牛牛&amp;blog">
<meta property="og:description" content="工作中常用的lambda总结1. list转map工作中，我们经常遇到list转map的案例。Collectors.toMap就可以把一个list数组转成一个Map。代码如下： public class TestLambda &amp;#123;    public static void main(String[] args) &amp;#123;        List&lt;UserInfo&gt; use">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="">
<meta property="article:published_time" content="2022-11-23T07:28:14.000Z">
<meta property="article:modified_time" content="2023-11-02T03:32:07.214Z">
<meta property="article:author" content="牛牛">
<meta property="article:tag" content="java8">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content=""><link rel="shortcut icon" href="/img/myfavicon.png"><link rel="canonical" href="https://code-ran.gitee.io/2022/11/23/%E5%B7%A5%E4%BD%9C%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%9A%84lambda%E6%80%BB%E7%BB%93/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '工作中常用的lambda总结',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-11-02 11:32:07'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/style.css"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/ys.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">牛牛&amp;blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">工作中常用的lambda总结</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-11-23T07:28:14.000Z" title="发表于 2022-11-23 15:28:14">2022-11-23</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-02T03:32:07.214Z" title="更新于 2023-11-02 11:32:07">2023-11-02</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/java8%E7%9B%B8%E5%85%B3/">java8相关</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="工作中常用的lambda总结"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h3 id="工作中常用的lambda总结"><a href="#工作中常用的lambda总结" class="headerlink" title="工作中常用的lambda总结"></a>工作中常用的lambda总结</h3><h4 id="1-list转map"><a href="#1-list转map" class="headerlink" title="1. list转map"></a>1. list转map</h4><p>工作中，我们经常遇到<code>list</code>转<code>map</code>的案例。<code>Collectors.toMap</code>就可以把一个<code>list</code>数组转成一个<code>Map</code>。代码如下：</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">TestLambda</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span>(<span class="params">String[] args</span>)</span> &#123;</span><br><span class="line"></span><br><span class="line">        List&lt;UserInfo&gt; userInfoList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">1L</span>, <span class="string">&quot;捡田螺的小男孩&quot;</span>, <span class="number">18</span>));</span><br><span class="line">        userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">2L</span>, <span class="string">&quot;程序员田螺&quot;</span>, <span class="number">27</span>));</span><br><span class="line">        userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">2L</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>, <span class="number">26</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         *  list 转 map</span></span><br><span class="line"><span class="comment">         *  使用Collectors.toMap的时候，如果有可以重复会报错，所以需要加(k1, k2) -&gt; k1</span></span><br><span class="line"><span class="comment">         *  (k1, k2) -&gt; k1 表示，如果有重复的key,则保留第一个，舍弃第二个</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        Map&lt;Long, UserInfo&gt; userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUserId, userInfo -&gt; userInfo, (k1, k2) -&gt; k1));</span><br><span class="line">        userInfoMap.values().forEach(a-&gt;System.<span class="keyword">out</span>.println(a.getUserName()));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//运行结果</span></span><br><span class="line">捡田螺的小男孩</span><br><span class="line">程序员田螺</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>类似的，还有<code>Collectors.toList()</code>、<code>Collectors.toSet()</code>，表示把对应的流转化为<code>list</code>或者<code>Set</code>。</p>
<h4 id="2-filter-过滤"><a href="#2-filter-过滤" class="headerlink" title="2. filter()过滤"></a>2. filter()过滤</h4><p>从数组集合中，过滤掉不符合条件的元素，留下符合条件的元素。</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">List&lt;UserInfo&gt; userInfoList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">1L</span>, <span class="string">&quot;捡田螺的小男孩&quot;</span>, <span class="number">18</span>));</span><br><span class="line">userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">2L</span>, <span class="string">&quot;程序员田螺&quot;</span>, <span class="number">27</span>));</span><br><span class="line">userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">3L</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>, <span class="number">26</span>));</span><br><span class="line">        </span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * filter 过滤，留下超过18岁的用户</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">List&lt;UserInfo&gt; userInfoResultList = userInfoList.stream().filter(user -&gt; user.getAge() &gt; <span class="number">18</span>).collect(Collectors.toList());</span><br><span class="line">userInfoResultList.forEach(a -&gt; System.<span class="keyword">out</span>.println(a.getUserName()));</span><br><span class="line">        </span><br><span class="line"><span class="comment">//运行结果</span></span><br><span class="line">程序员田螺</span><br><span class="line">捡瓶子的小男孩</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="3-foreach遍历"><a href="#3-foreach遍历" class="headerlink" title="3. foreach遍历"></a>3. foreach遍历</h4><p>foreach 遍历list，遍历map，真的很丝滑。</p>
<figure class="highlight arduino"><table><tr><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * forEach 遍历集合List列表</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">List&lt;<span class="keyword">String</span>&gt; userNameList = Arrays.<span class="built_in">asList</span>(<span class="string">&quot;捡田螺的小男孩&quot;</span>, <span class="string">&quot;程序员田螺&quot;</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>);</span><br><span class="line">userNameList.forEach(System.out::println);</span><br><span class="line"> </span><br><span class="line">HashMap&lt;<span class="keyword">String</span>, <span class="keyword">String</span>&gt; hashMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">hashMap.<span class="built_in">put</span>(<span class="string">&quot;公众号&quot;</span>, <span class="string">&quot;捡田螺的小男孩&quot;</span>);</span><br><span class="line">hashMap.<span class="built_in">put</span>(<span class="string">&quot;职业&quot;</span>, <span class="string">&quot;程序员田螺&quot;</span>);</span><br><span class="line">hashMap.<span class="built_in">put</span>(<span class="string">&quot;昵称&quot;</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>);</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *  forEach 遍历集合Map</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">hashMap.forEach((k, v) -&gt; System.out.<span class="built_in">println</span>(k + <span class="string">&quot;:\t&quot;</span> + v));</span><br><span class="line"></span><br><span class="line"><span class="comment">//运行结果</span></span><br><span class="line">捡田螺的小男孩</span><br><span class="line">程序员田螺</span><br><span class="line">捡瓶子的小男孩</span><br><span class="line">职业:	程序员田螺</span><br><span class="line">公众号:	捡田螺的小男孩</span><br><span class="line">昵称:	捡瓶子的小男孩</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="4-groupingBy分组"><a href="#4-groupingBy分组" class="headerlink" title="4. groupingBy分组"></a>4. groupingBy分组</h4><p>提到分组，相信大家都会想起<code>SQL</code>的<code>group by</code>。我们经常需要一个List做分组操作。比如，按城市分组用户。在Java8之前，是这么实现的：</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">List&lt;UserInfo&gt; <span class="attr">originUserInfoList</span> = new ArrayList&lt;&gt;()<span class="comment">;</span></span><br><span class="line">originUserInfoList.add(new UserInfo(1L, &quot;捡田螺的小男孩&quot;, 18,&quot;深圳&quot;))<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">originUserInfoList.add(new UserInfo(3L, &quot;捡瓶子的小男孩&quot;, 26,&quot;湛江&quot;))<span class="comment">;</span></span><br><span class="line">originUserInfoList.add(new UserInfo(2L, &quot;程序员田螺&quot;, 27,&quot;深圳&quot;))<span class="comment">;</span></span><br><span class="line">Map&lt;String, List&lt;UserInfo&gt;&gt; <span class="attr">result</span> = new HashMap&lt;&gt;()<span class="comment">;</span></span><br><span class="line">for (UserInfo userInfo : originUserInfoList) &#123;</span><br><span class="line">  String <span class="attr">city</span> = userInfo.getCity()<span class="comment">;</span></span><br><span class="line">  List&lt;UserInfo&gt; <span class="attr">userInfos</span> = result.get(city)<span class="comment">;</span></span><br><span class="line">  if (<span class="attr">userInfos</span> == null) &#123;</span><br><span class="line">      <span class="attr">userInfos</span> = new ArrayList&lt;&gt;()<span class="comment">;</span></span><br><span class="line">      result.put(city, userInfos)<span class="comment">;</span></span><br><span class="line">    &#125;</span><br><span class="line">  userInfos.add(userInfo)<span class="comment">;</span></span><br><span class="line">&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>而使用Java8的<code>groupingBy</code>分组器，清爽无比:</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">Map&lt;String, List&lt;UserInfo&gt;&gt; <span class="attr">result</span> = originUserInfoList.stream()</span><br><span class="line">.collect(Collectors.groupingBy(UserInfo::getCity))<span class="comment">;</span></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="5-sorted-Comparator-排序"><a href="#5-sorted-Comparator-排序" class="headerlink" title="5. sorted+Comparator 排序"></a>5. sorted+Comparator 排序</h4><p>工作中，排序的需求比较多，使用sorted+Comparator排序，真的很香。</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">List&lt;UserInfo&gt; <span class="attr">userInfoList</span> = new ArrayList&lt;&gt;()<span class="comment">;</span></span><br><span class="line">userInfoList.add(new UserInfo(1L, &quot;捡田螺的小男孩&quot;, 18))<span class="comment">;</span></span><br><span class="line">userInfoList.add(new UserInfo(3L, &quot;捡瓶子的小男孩&quot;, 26))<span class="comment">;</span></span><br><span class="line">userInfoList.add(new UserInfo(2L, &quot;程序员田螺&quot;, 27))<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> *  sorted + Comparator.comparing 排序列表，</span><br><span class="line"> */</span><br><span class="line"><span class="attr">userInfoList</span> = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge)).collect(Collectors.toList())<span class="comment">;</span></span><br><span class="line">userInfoList.forEach(a -&gt; System.out.println(a.toString()))<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">System.out.println(&quot;开始降序排序&quot;)<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * 如果想降序排序，则可以使用加reversed()</span><br><span class="line"> */</span><br><span class="line"><span class="attr">userInfoList</span> = userInfoList.stream().sorted(Comparator.comparing(UserInfo::getAge).reversed()).collect(Collectors.toList())<span class="comment">;</span></span><br><span class="line">userInfoList.forEach(a -&gt; System.out.println(a.toString()))<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">//运行结果</span><br><span class="line">UserInfo&#123;<span class="attr">userId</span>=<span class="number">1</span>, userName=<span class="string">&#x27;捡田螺的小男孩&#x27;</span>, age=<span class="number">18</span>&#125;</span><br><span class="line">UserInfo&#123;<span class="attr">userId</span>=<span class="number">3</span>, userName=<span class="string">&#x27;捡瓶子的小男孩&#x27;</span>, age=<span class="number">26</span>&#125;</span><br><span class="line">UserInfo&#123;<span class="attr">userId</span>=<span class="number">2</span>, userName=<span class="string">&#x27;程序员田螺&#x27;</span>, age=<span class="number">27</span>&#125;</span><br><span class="line">开始降序排序</span><br><span class="line">UserInfo&#123;<span class="attr">userId</span>=<span class="number">2</span>, userName=<span class="string">&#x27;程序员田螺&#x27;</span>, age=<span class="number">27</span>&#125;</span><br><span class="line">UserInfo&#123;<span class="attr">userId</span>=<span class="number">3</span>, userName=<span class="string">&#x27;捡瓶子的小男孩&#x27;</span>, age=<span class="number">26</span>&#125;</span><br><span class="line">UserInfo&#123;<span class="attr">userId</span>=<span class="number">1</span>, userName=<span class="string">&#x27;捡田螺的小男孩&#x27;</span>, age=<span class="number">18</span>&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="6-distinct去重"><a href="#6-distinct去重" class="headerlink" title="6.distinct去重"></a>6.distinct去重</h4><p><code>distinct</code>可以去除重复的元素:</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">List&lt;String&gt; <span class="attr">list</span> = Arrays.asList(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;F&quot;</span>, <span class="string">&quot;A&quot;</span>, <span class="string">&quot;C&quot;</span>)<span class="comment">;</span></span><br><span class="line">List&lt;String&gt; <span class="attr">temp</span> = list.stream().distinct().collect(Collectors.toList())<span class="comment">;</span></span><br><span class="line">temp.forEach(System.out::println)<span class="comment">;</span></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="7-findFirst-返回第一个"><a href="#7-findFirst-返回第一个" class="headerlink" title="7. findFirst 返回第一个"></a>7. findFirst 返回第一个</h4><p><code>findFirst</code> 很多业务场景，我们只需要返回集合的第一个元素即可：</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">List&lt;String&gt; <span class="attr">list</span> = Arrays.asList(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;F&quot;</span>, <span class="string">&quot;A&quot;</span>, <span class="string">&quot;C&quot;</span>)<span class="comment">;</span></span><br><span class="line">list.stream().findFirst().ifPresent(System.out::println)<span class="comment">;</span></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="8-anyMatch是否至少匹配一个元素"><a href="#8-anyMatch是否至少匹配一个元素" class="headerlink" title="8. anyMatch是否至少匹配一个元素"></a>8. anyMatch是否至少匹配一个元素</h4><p><code>anyMatch</code> 检查流是否包含至少一个满足给定谓词的元素。</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">Stream&lt;String&gt; <span class="attr">stream</span> = Stream.of(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>, <span class="string">&quot;D&quot;</span>)<span class="comment">;</span></span><br><span class="line">boolean <span class="attr">match</span> = stream.anyMatch(s -&gt; s.contains(<span class="string">&quot;C&quot;</span>))<span class="comment">;</span></span><br><span class="line">System.out.println(match)<span class="comment">;</span></span><br><span class="line">//输出</span><br><span class="line">true</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="9-allMatch-匹配所有元素"><a href="#9-allMatch-匹配所有元素" class="headerlink" title="9. allMatch 匹配所有元素"></a>9. allMatch 匹配所有元素</h4><p><code>allMatch</code> 检查流是否满足所有给定谓词的元素。</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">Stream&lt;String&gt; <span class="attr">stream</span> = Stream.of(<span class="string">&quot;A&quot;</span>, <span class="string">&quot;B&quot;</span>, <span class="string">&quot;C&quot;</span>, <span class="string">&quot;D&quot;</span>)<span class="comment">;</span></span><br><span class="line">boolean <span class="attr">match</span> = stream.allMatch(s -&gt; s.contains(<span class="string">&quot;C&quot;</span>))<span class="comment">;</span></span><br><span class="line">System.out.println(match)<span class="comment">;</span></span><br><span class="line">//输出</span><br><span class="line">false</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="10-map转换"><a href="#10-map转换" class="headerlink" title="10. map转换"></a>10. map转换</h4><p><code>map</code>方法可以帮我们做元素转换，比如一个元素所有字母转化为大写，又或者把获取一个元素对象的某个属性，<code>demo</code>如下：</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">List&lt;String&gt; <span class="attr">list</span> = Arrays.asList(<span class="string">&quot;jay&quot;</span>, <span class="string">&quot;tianluo&quot;</span>)<span class="comment">;</span></span><br><span class="line">//转化为大写</span><br><span class="line">List&lt;String&gt; <span class="attr">upperCaselist</span> = list.stream().map(String::toUpperCase).collect(Collectors.toList())<span class="comment">;</span></span><br><span class="line">upperCaselist.forEach(System.out::println)<span class="comment">;</span></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h2 id="11-Reduce"><a href="#11-Reduce" class="headerlink" title="11. Reduce"></a>11. Reduce</h2><p>Reduce可以合并流的元素，并生成一个值</p>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line">int sum = Stream<span class="selector-class">.of</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)<span class="selector-class">.reduce</span>(<span class="number">0</span>, (<span class="selector-tag">a</span>, <span class="selector-tag">b</span>) -&gt; <span class="selector-tag">a</span> + <span class="selector-tag">b</span>);</span><br><span class="line">System<span class="selector-class">.out</span><span class="selector-class">.println</span>(sum);</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="12-peek-打印个日志"><a href="#12-peek-打印个日志" class="headerlink" title="12. peek 打印个日志"></a>12. peek 打印个日志</h4><p><code>peek()</code>方法是一个中间<code>Stream</code>操作，有时候我们可以使用<code>peek</code>来打印日志。</p>
<figure class="highlight rust"><table><tr><td class="code"><pre><span class="line">List&lt;<span class="built_in">String</span>&gt; result = Stream.of(<span class="string">&quot;程序员田螺&quot;</span>, <span class="string">&quot;捡田螺的小男孩&quot;</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>)</span><br><span class="line">            .filter(a -&gt; a.contains(<span class="string">&quot;田螺&quot;</span>))</span><br><span class="line">            .peek(a -&gt; System.out.println(<span class="string">&quot;关注公众号:&quot;</span> + a)).collect(Collectors.toList());</span><br><span class="line">System.out.println(result);</span><br><span class="line"><span class="comment">//运行结果</span></span><br><span class="line">关注公众号:程序员田螺</span><br><span class="line">关注公众号:捡田螺的小男孩</span><br><span class="line">[程序员田螺, 捡田螺的小男孩]</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="13-Max，Min最大最小"><a href="#13-Max，Min最大最小" class="headerlink" title="13. Max，Min最大最小"></a>13. Max，Min最大最小</h4><p>使用lambda流求最大，最小值，非常方便。</p>
<figure class="highlight ini"><table><tr><td class="code"><pre><span class="line">List&lt;UserInfo&gt; <span class="attr">userInfoList</span> = new ArrayList&lt;&gt;()<span class="comment">;</span></span><br><span class="line">userInfoList.add(new UserInfo(1L, &quot;捡田螺的小男孩&quot;, 18))<span class="comment">;</span></span><br><span class="line">userInfoList.add(new UserInfo(3L, &quot;捡瓶子的小男孩&quot;, 26))<span class="comment">;</span></span><br><span class="line">userInfoList.add(new UserInfo(2L, &quot;程序员田螺&quot;, 27))<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">Optional&lt;UserInfo&gt; <span class="attr">maxAgeUserInfoOpt</span> = userInfoList.stream().max(Comparator.comparing(UserInfo::getAge))<span class="comment">;</span></span><br><span class="line">maxAgeUserInfoOpt.ifPresent(userInfo -&gt; System.out.println(&quot;max age user:&quot; + userInfo))<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">Optional&lt;UserInfo&gt; <span class="attr">minAgeUserInfoOpt</span> = userInfoList.stream().min(Comparator.comparing(UserInfo::getAge))<span class="comment">;</span></span><br><span class="line">minAgeUserInfoOpt.ifPresent(userInfo -&gt; System.out.println(&quot;min age user:&quot; + userInfo))<span class="comment">;</span></span><br><span class="line"></span><br><span class="line">//运行结果</span><br><span class="line">max gae user:UserInfo&#123;<span class="attr">userId</span>=<span class="number">2</span>, userName=<span class="string">&#x27;程序员田螺&#x27;</span>, age=<span class="number">27</span>&#125;</span><br><span class="line">min gae user:UserInfo&#123;<span class="attr">userId</span>=<span class="number">1</span>, userName=<span class="string">&#x27;捡田螺的小男孩&#x27;</span>, age=<span class="number">18</span>&#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="14-count统计"><a href="#14-count统计" class="headerlink" title="14. count统计"></a>14. count统计</h4><p>一般<code>count()</code>表示获取流数据元素总数。</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">List&lt;UserInfo&gt; userInfoList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">1L</span>, <span class="string">&quot;捡田螺的小男孩&quot;</span>, <span class="number">18</span>));</span><br><span class="line">userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">3L</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>, <span class="number">26</span>));</span><br><span class="line">userInfoList.<span class="keyword">add</span>(<span class="keyword">new</span> UserInfo(<span class="number">2L</span>, <span class="string">&quot;程序员田螺&quot;</span>, <span class="number">27</span>));</span><br><span class="line"></span><br><span class="line"><span class="built_in">long</span> count = userInfoList.stream().filter(user -&gt; user.getAge() &gt; <span class="number">18</span>).count();</span><br><span class="line">System.<span class="keyword">out</span>.println(<span class="string">&quot;大于18岁的用户:&quot;</span> + count);</span><br><span class="line"><span class="comment">//输出</span></span><br><span class="line">大于<span class="number">18</span>岁的用户:<span class="number">2</span></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<h4 id="15-常用函数式接口"><a href="#15-常用函数式接口" class="headerlink" title="15. 常用函数式接口"></a>15. 常用函数式接口</h4><p>其实lambda离不开函数式接口，我们来看下JDK8常用的几个函数式接口：</p>
<ul>
<li><code>Function&lt;T, R&gt;</code>（转换型）: 接受一个输入参数，返回一个结果</li>
<li><code>Consumer&lt;T&gt;</code> （消费型）: 接收一个输入参数，并且无返回操作</li>
<li><code>Predicate&lt;T&gt;</code> （判断型）: 接收一个输入参数，并且返回布尔值结果</li>
<li><code>Supplier&lt;T&gt;</code> （供给型）: 无参数，返回结果</li>
</ul>
<p><code>Function&lt;T, R&gt;</code> 是一个功能转换型的接口，可以把将一种类型的数据转化为另外一种类型的数据</p>
<figure class="highlight arduino"><table><tr><td class="code"><pre><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">testFunction</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//获取每个字符串的长度，并且返回</span></span><br><span class="line">        Function&lt;<span class="keyword">String</span>, Integer&gt; function = <span class="keyword">String</span>::length;</span><br><span class="line">        <span class="built_in">Stream</span>&lt;<span class="keyword">String</span>&gt; stream = <span class="built_in">Stream</span>.<span class="built_in">of</span>(<span class="string">&quot;程序员田螺&quot;</span>, <span class="string">&quot;捡田螺的小男孩&quot;</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>);</span><br><span class="line">        <span class="built_in">Stream</span>&lt;Integer&gt; resultStream = stream.<span class="built_in">map</span>(function);</span><br><span class="line">        resultStream.forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p><code>Consumer&lt;T&gt;</code>是一个消费性接口，通过传入参数，并且无返回的操作</p>
<figure class="highlight arduino"><table><tr><td class="code"><pre><span class="line">   <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">testComsumer</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//获取每个字符串的长度，并且返回</span></span><br><span class="line">        Consumer&lt;<span class="keyword">String</span>&gt; comsumer = System.out::println;</span><br><span class="line">        <span class="built_in">Stream</span>&lt;<span class="keyword">String</span>&gt; stream = <span class="built_in">Stream</span>.<span class="built_in">of</span>(<span class="string">&quot;程序员田螺&quot;</span>, <span class="string">&quot;捡田螺的小男孩&quot;</span>, <span class="string">&quot;捡瓶子的小男孩&quot;</span>);</span><br><span class="line">        stream.forEach(comsumer);</span><br><span class="line">    &#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p><code>Predicate&lt;T&gt;</code>是一个判断型接口,并且返回布尔值结果.</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">testPredicate</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">//获取每个字符串的长度，并且返回</span></span><br><span class="line">        Predicate&lt;Integer&gt; predicate = a -&gt; a &gt; <span class="number">18</span>;</span><br><span class="line">        UserInfo userInfo = <span class="keyword">new</span> UserInfo(<span class="number">2L</span>, <span class="string">&quot;程序员田螺&quot;</span>, <span class="number">27</span>);</span><br><span class="line">        System.<span class="keyword">out</span>.println(predicate.test(userInfo.getAge()));</span><br><span class="line">    &#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p><code>Supplier&lt;T&gt;</code>是一个供给型接口,无参数，有返回结果。</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">testSupplier</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        Supplier&lt;Integer&gt; supplier = () -&gt; Integer.valueOf(<span class="string">&quot;666&quot;</span>);</span><br><span class="line">        System.<span class="keyword">out</span>.println(supplier.<span class="keyword">get</span>());</span><br><span class="line">    &#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure>

<p>这几个函数在日常开发中，也是可以灵活应用的，比如我们DAO操作完数据库，是会有个result的整型结果返回。我们就可以用<code>Supplier&lt;T&gt;</code>来统一判断是否操作成功。如下：</p>
<figure class="highlight csharp"><table><tr><td class="code"><pre><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">saveDb</span>(<span class="params">Supplier&lt;Integer&gt; supplier</span>)</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (supplier.<span class="keyword">get</span>() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            System.<span class="keyword">out</span>.println(<span class="string">&quot;插入数据库成功&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            System.<span class="keyword">out</span>.println(<span class="string">&quot;插入数据库失败&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    @Test</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span>(<span class="params"></span>) throws Exception</span> &#123;</span><br><span class="line">        Course course=<span class="keyword">new</span> Course();</span><br><span class="line">        course.setCname(<span class="string">&quot;java&quot;</span>);</span><br><span class="line">        course.setUserId(<span class="number">100L</span>);</span><br><span class="line">        course.setCstatus(<span class="string">&quot;Normal&quot;</span>);</span><br><span class="line">        saveDb(() -&gt; courseMapper.insert(course));</span><br><span class="line">    &#125;</span><br><span class="line">复制代码</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io">牛牛</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://code-ran.gitee.io/2022/11/23/%E5%B7%A5%E4%BD%9C%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%9A%84lambda%E6%80%BB%E7%BB%93/">https://code-ran.gitee.io/2022/11/23/%E5%B7%A5%E4%BD%9C%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%9A%84lambda%E6%80%BB%E7%BB%93/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://code-ran.gitee.io" target="_blank">牛牛&blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/java8/">java8</a></div><div class="post_share"><div class="social-share" data-image="" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/11/23/springboot%E8%AF%BB%E5%8F%96%E9%A1%B9%E7%9B%AE%E4%B8%8B%E7%9A%84%E6%96%87%E4%BB%B6/"><img class="prev-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">springboot读取项目下的文件</div></div></a></div><div class="next-post pull-right"><a href="/2022/11/23/java8%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/"><img class="next-cover" src="" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">java8函数式编程</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/11/23/java8Stream%E8%AF%A6%E8%A7%A3/" title="java8Stream详解"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-23</div><div class="title">java8Stream详解</div></div></a></div><div><a href="/2022/11/23/java8%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BC%96%E7%A8%8B/" title="java8函数式编程"><img class="cover" src="" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-11-23</div><div class="title">java8函数式编程</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/ys.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">牛牛</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">52</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">26</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/code-ran"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本blog内容仅用于个人学习使用!</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B7%A5%E4%BD%9C%E4%B8%AD%E5%B8%B8%E7%94%A8%E7%9A%84lambda%E6%80%BB%E7%BB%93"><span class="toc-number">1.</span> <span class="toc-text">工作中常用的lambda总结</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-list%E8%BD%ACmap"><span class="toc-number">1.1.</span> <span class="toc-text">1. list转map</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-filter-%E8%BF%87%E6%BB%A4"><span class="toc-number">1.2.</span> <span class="toc-text">2. filter()过滤</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-foreach%E9%81%8D%E5%8E%86"><span class="toc-number">1.3.</span> <span class="toc-text">3. foreach遍历</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-groupingBy%E5%88%86%E7%BB%84"><span class="toc-number">1.4.</span> <span class="toc-text">4. groupingBy分组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-sorted-Comparator-%E6%8E%92%E5%BA%8F"><span class="toc-number">1.5.</span> <span class="toc-text">5. sorted+Comparator 排序</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-distinct%E5%8E%BB%E9%87%8D"><span class="toc-number">1.6.</span> <span class="toc-text">6.distinct去重</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-findFirst-%E8%BF%94%E5%9B%9E%E7%AC%AC%E4%B8%80%E4%B8%AA"><span class="toc-number">1.7.</span> <span class="toc-text">7. findFirst 返回第一个</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-anyMatch%E6%98%AF%E5%90%A6%E8%87%B3%E5%B0%91%E5%8C%B9%E9%85%8D%E4%B8%80%E4%B8%AA%E5%85%83%E7%B4%A0"><span class="toc-number">1.8.</span> <span class="toc-text">8. anyMatch是否至少匹配一个元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-allMatch-%E5%8C%B9%E9%85%8D%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0"><span class="toc-number">1.9.</span> <span class="toc-text">9. allMatch 匹配所有元素</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-map%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.10.</span> <span class="toc-text">10. map转换</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-Reduce"><span class="toc-number"></span> <span class="toc-text">11. Reduce</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#12-peek-%E6%89%93%E5%8D%B0%E4%B8%AA%E6%97%A5%E5%BF%97"><span class="toc-number">0.1.</span> <span class="toc-text">12. peek 打印个日志</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#13-Max%EF%BC%8CMin%E6%9C%80%E5%A4%A7%E6%9C%80%E5%B0%8F"><span class="toc-number">0.2.</span> <span class="toc-text">13. Max，Min最大最小</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#14-count%E7%BB%9F%E8%AE%A1"><span class="toc-number">0.3.</span> <span class="toc-text">14. count统计</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#15-%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3"><span class="toc-number">0.4.</span> <span class="toc-text">15. 常用函数式接口</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/springboot%E6%B3%A8%E8%A7%A3%E7%9B%B8%E5%85%B3/" title="springboot注解相关">springboot注解相关</a><time datetime="2024-04-30T03:20:37.000Z" title="发表于 2024-04-30 11:20:37">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2024/04/30/CommandLineRunner%E5%92%8CApplicationRunner%E7%94%A8%E6%B3%95%E4%B8%8E%E5%8C%BA%E5%88%AB/" title="CommandLineRunner和ApplicationRunner用法与区别">CommandLineRunner和ApplicationRunner用法与区别</a><time datetime="2024-04-30T02:42:21.000Z" title="发表于 2024-04-30 10:42:21">2024-04-30</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/%E5%A5%BD%E6%96%87%E6%94%B6%E8%97%8F/" title="好文收藏">好文收藏</a><time datetime="2023-12-08T08:40:13.000Z" title="发表于 2023-12-08 16:40:13">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/08/springboot%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E4%B8%8B%E8%BD%BD/" title="springboot文件上传下载">springboot文件上传下载</a><time datetime="2023-12-08T03:30:54.000Z" title="发表于 2023-12-08 11:30:54">2023-12-08</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/12/05/linux%E4%BD%BF%E7%94%A8curl%E5%91%BD%E4%BB%A4%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82/" title="linux使用curl命令发送请求">linux使用curl命令发送请求</a><time datetime="2023-12-05T05:56:19.000Z" title="发表于 2023-12-05 13:56:19">2023-12-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2024 By 牛牛</div><div class="footer_custom_text">Hi, Welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">簡</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>